<?php
class Apps_Plugin_LangSelector extends Zend_Controller_Plugin_Abstract {
	public function preDispatch(Zend_Controller_Request_Abstract $request) {
	    $session = new Zend_Session_Namespace('sessionLanguage');

// 	    if(isset($session->lang)) {
//             if($session->lang != $request->getParam('lang')) {
//                 $lang = $request->getParam('lang');
//             } else {
//                 $lang = $session->lang;
//             }
// 	    } else {
// 	        $lang = $request->getParam('lang');
// 	    }

        $lang = $session->lang;

		if ($lang !== 'en' && $lang !== 'vi') {
			$lang = 'en';
		}

        $session->lang = $lang;

		if ($lang == 'en') {
			$locale = 'en_CA';
		} else {
			$locale = 'vi_CA';
		}

		$zl = new Zend_Locale();
		$zl->setLocale($locale);
		Zend_Registry::set('Zend_Locale', $zl);

		$translate = new Zend_Translate('csv', APPLICATION_PATH . '/configs/lang/'. $lang . '.csv' , $lang);
		Zend_Registry::set('Zend_Translate', $translate);
	}
}